//
//  DateViewController.m
//  FuelLog
//
//  Created by Pure Mass Communications on 4/5/09.
//  Copyright 2008 . All rights reserved.
//
//  Placed under GPL v2 with the kind permission of Pure Mass Communications
//
//  Modified to make compile under SDK 3.1 by Olaf Greck in May 2010
//
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#import "DateViewController.h"
#import "FuelLogAppDelegate.h"

@implementation DateViewController

@synthesize doneButton;
@synthesize editedObject, editedFieldKey, dateValue, text, label;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		// Initialization code
		dateSet = NO;
		firstChange = 0.0;
	}
	return self;
}

/*
 Implement loadView if you want to create a view hierarchy programmatically
- (void)loadView {
}
 */
 
 - (void)viewDidLoad { 
 [super viewDidLoad]; 
 doneButton = 	[[UIBarButtonItem alloc]  initWithBarButtonSystemItem:UIBarButtonSystemItemSave 
 target:self 
 action:@selector(done:)];
 
 self.navigationItem.rightBarButtonItem = doneButton;
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:YES];
	
	[self.navigationController setToolbarHidden:YES animated:YES];
	if (label != nil && datePicker != nil) {
		[label setText:text];
		[datePicker setDate:dateValue animated:NO];
	}
}
 

- (void)viewWillDisappear:(BOOL)animated {
	[datePicker resignFirstResponder];
}

#pragma mark -

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
	// Release anything that's not essential, such as cached data
}

- (void)dealloc {
	[datePicker release];
	[label release];
	[super dealloc];
}

#pragma mark -

- (IBAction)done:(id)sender {
	FuelLogAppDelegate *appDelegate = (FuelLogAppDelegate *)[[UIApplication sharedApplication] delegate];
	[[appDelegate dateSelected] setValue:[FuelLogAppDelegate getNoonUTC:datePicker.date] forKey:editedFieldKey];
	
//	[editedObject setValue:[FuelLogAppDelegate getNoonUTC:datePicker.date] forKey:editedFieldKey]; 
 
//	[self dismissModalViewControllerAnimated:YES];
	[self.navigationController popViewControllerAnimated:YES];
}


// Must connect this method to date picker's value changed event to make picker 
// initialize properly.
// Seems to be a bug in the simulator.  Not a problem on device.

#define SIMULATOR_BUG NO

- (IBAction)fixModalBug:(id)sender {
	if (!SIMULATOR_BUG) {
		dateSet = YES;
		return;
	}
	NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
	if (firstChange != 0.0 && now - firstChange > 1.0) {
		dateSet = YES;
	}
	if (!dateSet) {
		[datePicker setDate:dateValue animated:NO];
		if (firstChange == 0.0) {
			firstChange = now;
		}
	}
}

@end
